package com.example.petstore;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.adapter.ProductAdapter;
import com.example.petshopping.model.Product;

import java.util.ArrayList;
import java.util.List;

public class PetStoreDetailActivity extends AppCompatActivity {

    private ImageView ivBack;
    private Button btnFollow;
    private TextView tvStoreName;
    private TextView tvStoreRating;
    private TextView tvFollowers;
    private RecyclerView rvProducts;
    private ProductAdapter adapter;
    private List<Product> productList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pet_store_detail);
        
        initViews();
        setupListeners();
        handleIntent();
        initData();
        setupRecyclerView();
    }
    
    private void initViews() {
        ivBack = findViewById(R.id.iv_back);
        btnFollow = findViewById(R.id.btn_follow);
        tvStoreName = findViewById(R.id.tv_store_name);
        tvStoreRating = findViewById(R.id.tv_store_rating);
        tvFollowers = findViewById(R.id.tv_followers);
        rvProducts = findViewById(R.id.rv_products);
    }
    
    private void handleIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            // 获取从其他Activity传递过来的店铺信息
            String storeName = intent.getStringExtra("storeName");
            String storeRating = intent.getStringExtra("storeRating");
            String storeFollowers = intent.getStringExtra("storeFollowers");
            
            // 如果有数据，则更新UI
            if (storeName != null) {
                tvStoreName.setText(storeName);
            }
            
            if (storeRating != null) {
                tvStoreRating.setText(storeRating);
            }
            
            if (storeFollowers != null) {
                tvFollowers.setText(storeFollowers);
            }
        }
    }
    
    private void setupListeners() {
        ivBack.setOnClickListener(v -> finish());
        
        btnFollow.setOnClickListener(v -> {
            // 切换关注状态，这里只是简单演示
            if (btnFollow.getText().toString().equals("关注")) {
                btnFollow.setText("已关注");
                // 可以添加一些视觉效果或动画
            } else {
                btnFollow.setText("关注");
            }
        });
    }
    
    private void initData() {
        // 模拟数据
        for (int i = 0; i < 6; i++) {
            Product product = new Product();
            product.setId(String.valueOf(i + 1));
            product.setName("宠物标题" + (i + 1));
            product.setPrice("¥" + (900 + i * 10) + ".9");
            product.setImageUrl("yugang"); // 使用项目中已有的图片资源
            productList.add(product);
        }
    }
    
    private void setupRecyclerView() {
        adapter = new ProductAdapter(this, productList, R.layout.item_product_list);
        rvProducts.setLayoutManager(new GridLayoutManager(this, 2));
        rvProducts.setAdapter(adapter);
        
        // 设置点击事件监听
        adapter.setOnProductClickListener(new ProductAdapter.OnProductClickListener() {
            @Override
            public void onProductClick(Product product, int position) {
                // 处理商品点击事件
            }

            @Override
            public void onDeleteClick(Product product, int position) {
                // 不需要处理删除点击事件
            }
        });
    }
} 